package com.karaoke.android.player

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import tv.danmaku.ijk.media.player.IjkMediaPlayer

class MediaPlayerService : Service()
{
    var mPlayer: IjkMediaPlayer? = null

    override fun onBind(intent: Intent): IBinder?
    {
        return LocalBinder()
    }

    public inner class LocalBinder : Binder()
    {
        val service: MediaPlayerService
            get() = this@MediaPlayerService
    }

    fun newMediaPlayer(): IjkMediaPlayer
    {
        mPlayer?.let {
            it.stop()
            it.release()
        }
        mPlayer = IjkMediaPlayer()
        IjkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_SILENT)
        mPlayer?.apply {
            setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 0)
            setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "opensles", 0)
            setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 1)
            setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0)
            setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "http-detect-range-support", 0)
            setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-hevc", 1)
            setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1)
        }
        return mPlayer!!
    }
}
